package com.ruoyi.zsix.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.zsix.domain.model.SmsEntity;
import com.ruoyi.zsix.mapper.taskAssigneeMapper.SmsEntityMapper;
import com.ruoyi.zsix.service.ISmsEntityService;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.task.model.Task;
import org.activiti.engine.history.HistoricProcessInstance;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class ISmsEntityServiceImpl implements ISmsEntityService {
    @Autowired
    private SmsEntityMapper smsEntityMapper;

    /**
     * 获取流程通知列表
     * @param sms
     * @return
     */
    @Override
    public List<SmsEntity> smsList(SmsEntity sms){
        List<SmsEntity> smsEntityList=smsEntityMapper.smsList(SecurityUtils.getUsername(),sms.getState());
        return smsEntityList;
    }
    /**
     * 获取流程知会列表
     * @param sms
     * @return
     */
    @Override
    public List<SmsEntity> ccSmsList(SmsEntity sms){
        List<SmsEntity> smsEntityList=smsEntityMapper.ccSmsList(SecurityUtils.getUsername(),sms.getState());
        return smsEntityList;
    }
    /**
     * 设置消息已读
     * @param sms
     * @return
     */
    @Override
    public SmsEntity updateState(SmsEntity sms){
        smsEntityMapper.updateState(sms.getId());
        return sms;
    }

    /**
     * 删除消息
     * @param sms
     * @return
     */
    @Override
    public SmsEntity deleteSms(SmsEntity sms){
        smsEntityMapper.deleteSms(sms.getId());
        return sms;
    }
    /**
     * 发送流程审核消息
     * @param toUsers
     * @param sender
     * @param processInstance
     * @param task
     * @param historicProcessInstance
     * @param endTask
     * @return
     */
    @Override
    public String sendFlowMsg(String toUsers, String sender, ProcessInstance processInstance
            , Task task, HistoricProcessInstance historicProcessInstance, Boolean endTask){
        if(!endTask) {
            if (!toUsers.contains(",")) {
                SmsEntity smsEntity = new SmsEntity();
                smsEntity.setProce_inst_id(task.getProcessInstanceId());
                smsEntity.setId(UUID.randomUUID().toString());
                smsEntity.setMsgCont("流程[" + processInstance.getName() + "][" + task.getName() + "]步骤已完成，目前需要您进行审核");
                smsEntity.setSender(sender);
                smsEntity.setState("0");
                smsEntity.setToUser(toUsers);
                smsEntity.setSmsType("流程审核");
                smsEntity.setRdt(DateTime.now().toString("yyyy-MM-dd hh:mm:ss"));
                smsEntity.setTitle("您有一项流程待办需要审核");
                smsEntityMapper.addSms(smsEntity);
            } else {
                String[] users = toUsers.split(",");
                for (String user : users) {
                    SmsEntity smsEntity = new SmsEntity();
                    smsEntity.setProce_inst_id(task.getProcessInstanceId());
                    smsEntity.setId(UUID.randomUUID().toString());
                    smsEntity.setMsgCont("流程[" + processInstance.getName() + "][" + task.getName() + "]步骤已完成，目前需要您进行审核");
                    smsEntity.setSender(sender);
                    smsEntity.setState("0");
                    smsEntity.setToUser(user);
                    smsEntity.setSmsType("流程审核");
                    smsEntity.setRdt(DateTime.now().toString("yyyy-MM-dd hh:mm:ss"));
                    smsEntity.setTitle("您有一项流程待办需要审核");
                    smsEntityMapper.addSms(smsEntity);
                }
            }
        }
        else{
            SmsEntity smsEntity = new SmsEntity();
            smsEntity.setProce_inst_id(task.getProcessInstanceId());
            smsEntity.setId(UUID.randomUUID().toString());
            smsEntity.setMsgCont("您的["+processInstance.getName()+"]流程申请已经审核通过。");
            smsEntity.setSender(sender);
            smsEntity.setState("0");
            smsEntity.setToUser(historicProcessInstance.getStartUserId());
            smsEntity.setSmsType("流程通知");
            smsEntity.setRdt(DateTime.now().toString("yyyy-MM-dd hh:mm:ss"));
            smsEntity.setTitle("审核结果通知");
            smsEntityMapper.addSms(smsEntity);
        }
        return "发送成功";
    }
}
